﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using WeifenLuo.WinFormsUI.Docking;

using Plugin;
using DependencyInjection;
using PolarExpress.View.Interfaces;

namespace PolarExpress.View
{
    public partial class FrmToolboxView : DockContent, IToolboxView
    {
        public event PluginHandler PluginDrag;

        PluginManager _pluginManager = null;

        Dictionary<string, Image> _imagesByName = new Dictionary<string, Image>();
        Dictionary<string, int> _imageIndexesByName = new Dictionary<string, int>();

        ImageList _imageList = new ImageList();

        [Inject]
        public FrmToolboxView(PluginManager pluginManager)
        {
            InitializeComponent();

            _imageList.ColorDepth = ColorDepth.Depth32Bit;
            _imageList.ImageSize = new System.Drawing.Size(32, 32);
            
            treeView1.ImageList = _imageList;

            _pluginManager = pluginManager;
            _pluginManager.Loaded += new PluginManager.LoadedHandler(_pluginManager_Loaded);
            _pluginManager.InitializePlugins();

            treeView1.ItemDrag += new ItemDragEventHandler(treeView_ItemDrag);
        }
        

        private void OnComponentDrag(ItemDragEventArgs e)
        {
            if (((TreeNode)e.Item).Tag is Plugin.Plugin)
            {
                Plugin.Plugin plugin = ((TreeNode)e.Item).Tag as Plugin.Plugin;
                treeView1.DoDragDrop(plugin, DragDropEffects.Copy);

                if (PluginDrag != null)
                {
                    PluginDrag(new PluginEventArgs(plugin, 0,0));
                }
            }
        }

        private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            OnComponentDrag(e);
        }

        void _pluginManager_Loaded(object sender, PluginLoadedEventArgs args)
        {
            foreach (Plugin.Plugin plugin in _pluginManager.Plugins)
            {
                TreeNode tnClass = new TreeNode(plugin.ClassAttribute.Name);
                tnClass.Tag = plugin;
                tnClass.SelectedImageIndex = tnClass.ImageIndex = GetImageIndex(plugin.ClassAttribute.Image, plugin.ClassAttribute.IconName);

                foreach (FieldAttribute f in plugin.FieldAttributes)
                {
                    TreeNode tnField = new TreeNode(f.Name);
                    tnField.SelectedImageIndex = tnField.ImageIndex = GetImageIndex(f.Image, f.IconName);

                    tnClass.Nodes.Add(tnField);
                }

                foreach (EventAttribute e in plugin.EventAttributes)
                {
                    TreeNode tnEvent = new TreeNode(e.Name);
                    tnEvent.SelectedImageIndex = tnEvent.ImageIndex = GetImageIndex(e.Image, e.IconName);
                    
                    tnClass.Nodes.Add(tnEvent);
                }

                foreach (MethodAttribute m in plugin.MethodAttributes)
                {
                    TreeNode tnMethod = new TreeNode(m.Name);
                    tnMethod.SelectedImageIndex = tnMethod.ImageIndex = GetImageIndex(m.Image, m.IconName);

                    tnClass.Nodes.Add(tnMethod);
                }

                treeView1.Nodes.Add(tnClass);

                treeView1.ExpandAll();
            } 
        }

        protected int GetImageIndex(Image image, string name)
        {
            int index = 0;

            if (_imagesByName.ContainsKey(name))
            {
                index = _imageIndexesByName[name];
            }
            else
            {
                index = _imageList.Images.Count;
                _imageList.Images.Add(image);
                _imagesByName.Add(name, image);
                _imageIndexesByName.Add(name, index);
            }

            return index;
        }
    }
}
